package com.kingtroldata.gyjk_demo;

import android.graphics.Bitmap;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;

import com.google.gson.Gson;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.callback.BitmapCallback;
import com.lzy.okgo.callback.StringCallback;

import java.util.List;

import okhttp3.Call;
import okhttp3.Response;

public class MainActivity extends AppCompatActivity {

    private ImageView bg;
//    private float s_width = 1136.0F;
//    private float s_height = 542.0F;

    private float s_width = 1120.0F;
    private float s_height = 539.0F;

    private float skew_x = 0;//x轴偏移量
    private float skew_y = 0;//y轴偏移量

//    底图的宽高为:  bg_height:905.0;bg_width:1794.0 x轴缩放比:0.63322186;y轴缩放比:0.598895
//    底图的宽高为:  bg_height:1005.0;bg_width:1794.0x轴缩放比:0.63322186;y轴缩放比:0.5393035

    private FrameLayout fl;
    private ScreenKit screenKit;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        bg = (ImageView) findViewById(R.id.iv_bg);
        fl = (FrameLayout) findViewById(R.id.fl);

        getBG();

    }

    @Override
    protected void onResume() {
        super.onResume();
        initSize();
    }

    private void initSize() {
        screenKit = ScreenKit.init(bg, s_width, s_height, skew_x, skew_y)
                .setOnMessureSuccessListener(new ScreenKit.MeasureSuccessCallBack() {
                    @Override
                    public void onMeasureSuccess() {
                        getData();
                    }
                });
    }

    //获取背景图片
    private void getBG() {
        OkGo.get(URLS.BG)
                .tag(this)
                .execute(new BitmapCallback() {
                    @Override
                    public void onSuccess(Bitmap bitmap, Call call, Response response) {
                        bg.setImageBitmap(bitmap);
                    }
                });
    }

    //获取测试点的数据
    public void getData(String data) {
        Gson gson = new Gson();
        Bean bean = gson.fromJson(data, Bean.class);
        List<Bean.PicBean> picBeanList = bean.pic;
        for (Bean.PicBean b : picBeanList) {
            String monitorLocationBg = b.monitorLocationBg;//图片的地址
            float imgWidth = b.monitorLocationImgWidth;//图片的宽
            float imgHeight = b.monitorLocationImgHeight;//图片的高
            float monitorLocationX = b.monitorLocationX;//x轴的坐标
            float monitorLocationY = b.monitorLocationY;//y轴的坐标

            setPic(monitorLocationBg, imgWidth, imgHeight, monitorLocationX, monitorLocationY);
        }
    }

    //将图片设置上去
    private void setPic(String picUrl, final float imgWidth, final float imgHeight, final float monitorLocationX, final float monitorLocationY) {
        OkGo.get(picUrl)
                .tag(this)
                .execute(new BitmapCallback() {
                    @Override
                    public void onSuccess(Bitmap bitmap, Call call, Response response) {
                        ImageView iv = new ImageView(MainActivity.this);
                        iv.setImageBitmap(bitmap);
                        View view = screenKit.transformView(iv, imgWidth, imgHeight, monitorLocationX, monitorLocationY);
                        fl.addView(view);
                        LoggerUtils.e("图片添加完成");
                    }
                });
    }

    private void getData() {
        OkGo.get(URLS.URL_GYJK)
                .tag(this)
                .params("factoryId", URLS.factoryId)
                .params("manageId", URLS.manageid)
                .execute(new StringCallback() {
                    @Override
                    public void onSuccess(String s, Call call, Response response) {
//                        LoggerUtils.e(s);
                        String replace = s.replace("\\r\\n", "");
//                        LoggerUtils.e(replace);
                        getData(replace);
                    }

                    @Override
                    public void onError(Call call, Response response, Exception e) {
                        super.onError(call, response, e);
                        LoggerUtils.e("网络请求失败");
                    }
                });
    }
}
